#!/bin/bash
###################
# author: wangs
# date: 2022-08-11
# dscript: arch backup
###################

# 定义检查执行用户的函数
function check_user(){
    uid=`id -u`
    if [[ $uid -ne "0" ]]; then
        echo -e "\033[31m\n01) 执行当前脚本用户,为普通用户,退出脚本\033[0m"
        exit 1
    else
        echo -e "\033[32m\n01) 执行当前脚本用户,为root用户,或sudo\n\033[0m"
    fi
}

# 定义检查磁盘剩余空间的函数
function check_disk(){
    echo -e "\033[32m\n02) 查看磁盘分区情况\n\033[0m"
    checkDisk=`df -T -m -x tmpfs -x devtmpfs | tail -n +2 | head -1 | awk '{print $5}'`
    if [[ $checkDisk -lt "15360" ]]; then
        echo -e "\033[31m\t/ 分区，可用空间 < 15G,退出脚本\033[0m"
        exit 1
    else
        checkDiskOk=$((${checkDisk}/1024))
        echo -e "\033[32m\t/ 分区,可用 $checkDiskOk GB\033[0m"
        checkDisk2=`df -T -m -x tmpfs -x devtmpfs | tail -n +2 | head -1 | awk '{print $4}'`
        checkDiskOk=$((${checkDisk2}/1024))
        echo -e "\033[32m\t/ 分区,已用 $checkDiskOk GB\n\033[0m"
    fi
}

# 检查挂载磁盘的函数
function check_mnt(){
    echo -e "\033[32m03) 检查是否存在挂载分区\033[0m"
    mntArray=('/mnt' '/media' '/run/mount' '/run/media')
    for ((i=0; i<${#mntArray[@]}; i++));
    do
        cehckCmd=`ls -l ${mntArray[i]} 2> /dev/null | wc -l`
        if [[ $cehckCmd -gt "1" ]]; then
            echo -e "\033[32m\t注意${mntArray[i]} 目录下,是否有磁盘挂载,退出脚本(y/N)\033[0m"
            read -n 1 -s choice
            case $choice in
            y)
                echo -e "\033[33m\t继续执行\033[0m"
            ;;
            *)
                echo -e "\033[31m\t${mntArray[i]} 目录下,可能有磁盘挂载,退出脚本\033[0m"
                echo
                exit 1
            ;;
            esac
        else
            echo -e "\033[32m\t${mntArray[i]} 目录下,没有挂载磁盘\033[0m"
        fi
    done
    # echo
}

# 定义检查pigz包的函数
function check_pigz(){
    pacman -Q pigz >/dev/null 2>&1
    if [[ $? -ne "0" ]]; then
        ping -i 0.1 -c 4 baidu.com >/dev/null 2>&1
        if [[ $? -ne "0" ]]; then
            echo -e "\033[31m04) 网络无法连接,请检查网络,退出脚本\033[0m"
            exit 1
        else
            sudo pacman -Syy --noconfirm  pigz >/dev/null 2>&1
            if [[ $? -ne "0" ]]; then
                echo -e "\033[31m\t安装 pigz包(多线程压缩),失败,退出脚本\033[0m"
                exit 1
            else
                echo -e "\033[32m04) 安装 pigz包(多线程压缩),成功\033[0m"
            fi
        fi
    else
        echo -e "\033[32m04) 已经安装 pigz包(多线程压缩)\033[0m"
    fi
    # echo
}

# 定义清理本地软件包缓存的函数
function clean_local_old(){
    echo -e "\033[32m05) 本地软件包缓存,是否继续清除(y/N)\033[0m"
    read -n 1 -s choice
    case $choice in
        y)
            sudo pacman -Qdtq >/dev/null 2>&1
            if [[ $? -ne "0" ]]; then
                echo -e "\033[32m\t不需要清理,不必要的依赖包\033[0m"
            else
                sudo pacman -Rs $(pacman -Qdtq) --noconfirm >/dev/null 2>&1
                echo -e "\033[33m\t清除,不必要的依赖包\033[0m"
            fi
            echo
            pacman -Q yay >/dev/null 2>&1
            if [[ $? -ne "0" ]]; then
                packInfoArr=('/var/cache/pacman/pkg/' '/var/lib/pacman/sync/')
                for ((i=0; i<${#packInfoArr[@]}; i++));
                do
                    checkPack=`ls -l ${packInfoArr[i]} 2> /dev/null | wc -l`
                    if [[ $checkPack -gt "1" ]]; then
                    sudo rm -rf ${packInfoArr[i]}* >/dev/null 2>&1
                    echo -e "\033[33m\t${packInfoArr[i]} 已清理\033[0m"
                else
                    echo -e "\033[32m\t${packInfoArr[i]} 不需要清理\033[0m"
                fi
            done
            else
                packInfoArr=('/var/cache/pacman/pkg/' '/var/lib/pacman/sync/' '/home/admin/.cache/yay/' '/tmp/yay/')
                for ((i=0; i<${#packInfoArr[@]}; i++));
                do
                    checkPack=`ls -l ${packInfoArr[i]} 2> /dev/null | wc -l`
                    if [[ $checkPack -gt "1" ]]; then
                        sudo rm -rf ${packInfoArr[i]}* >/dev/null 2>&1
                        echo -e "\033[33m\t${packInfoArr[i]} 已清理\033[0m"
                    else
                        echo -e "\033[32m\t${packInfoArr[i]} 不需要清理\033[0m"
                    fi
                done
            fi
            echo
        ;;
        *)
            echo -e "\033[33m\t不清理,不必要的依赖包,本地软件包缓存\033[0m"
            echo
        ;;
    esac
}

# 定义清理本地软件包缓存的函数
function clean_local(){
    echo -e "\033[32m05) 本地软件包缓存,是否继续清除(y/N)\033[0m"
    read -n 1 -s choice
    case $choice in
        y)
        sudo pacman -Qdtq >/dev/null 2>&1
            if [[ $? -ne "0" ]]; then
                echo -e "\033[32m\t不需要清理,不必要的依赖包\033[0m"
            else
                sudo pacman -Rs $(pacman -Qdtq) --noconfirm >/dev/null 2>&1
                echo -e "\033[33m\t清除,不必要的依赖包\033[0m"
            fi
            echo
            pacman -Q yay >/dev/null 2>&1
            if [[ $? -ne "0" ]]; then
                sudo pacman -Scc --noconfirm >/dev/null 2>&1
                echo -e "\033[33m\t执行 'pacman -Scc' 已清理\033[0m"
            else
                sudo yay -Scc --noconfirm >/dev/null 2>&1
                echo -e "\033[33m\t执行 'yay -Scc' 已清理\033[0m"
            fi
            echo
        ;;
        *)
            echo -e "\033[33m\t不清理,不必要的依赖包,本地软件包缓存\033[0m"
            echo
        ;;
    esac
}

# 定义进行系统备份的函数
function backup_linux(){
    echo -e "\033[32m06) 马上开始备份系统，是否继续 (y/N)\033[0m"
    read -n 1 -s choice
    case $choice in
        y)
            startDate=$(date +%s)
            backName=archBackup$(date +%Y%m%d)-$(date +%H%M%S).tgz
            cd /
            echo -e "\033[32m\t备份开始\033[0m"
            sudo tar --use-compress-program=pigz \
            -cvpf /$backName \
            --exclude=/$backName \
            --exclude=/lost+found \
            --exclude=/proc \
            --exclude=/sys \
            --exclude=/media \
            --exclude=/mnt \
            / 2> /dev/null
            echo -e "\033[32m\n\t备份完成\n\033[0m"
            endDate=$(date +%s)
            diffDate=$(( endDate - startDate ))
            echo -e "\033[33m\t备份时间: $diffDate 秒\n\033[0m"
            duCmd=`du -sh /$backName | awk '{print $1}'`
            echo -e "\033[33m\t备份的大小: $backName $duCmd\033[0m"
        ;;
        *)
            echo -e "\033[33m\t退出脚本\n\033[0m"
            exit 1
        ;;
    esac
}

# ========== main
check_user
check_disk
check_mnt
check_pigz
clean_local
backup_linux